package org.example.ch06.config;

import org.example.ch06.filter.MyFilter;
import org.example.ch06.filter.TestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName MvcConfig
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/14 19:17
 * @Version 1.0
 */
// 标识为配置类
@Configuration
/**
 * @ServletComponentScan注解是springboot提供的专门扫描servlet组件(servlet、Filter、Listener)
 * 注意：这种扫描有一个缺点，当有多个过滤器形成过滤连的时候，无法按照指定顺序来执行
*/
//@ServletComponentScan(basePackages = "org.example.ch06.filter")
public class MvcConfig {
    @Bean
    public FilterRegistrationBean TestFilter() {
        // 创建过滤器注册对象
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        // 设置过滤器
        registrationBean.setFilter(new TestFilter());
        // 设置过滤器名称
        registrationBean.setName("TestFilter");
        // 设置过滤器路径
        registrationBean.addUrlPatterns("/*");
        // 设置过滤器优先级,数值越小优先级越高
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean MyFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setName("MyFilter");
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(2);
        return registrationBean;
    }
}
